<template>
  <div class="business-list">
    <top-header></top-header>

    <div class="search-div">
      <div class="search-one-div">
        <search-one @search="onSearch"></search-one>
      </div>

      <span class="iconfont iconshaixuan"></span>
    </div>

    <div class="common-list-body" v-fullheight>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <a href="javascript:;" @click="goto(item)" class="list-item" v-for="(item, key) in listSource" :key="key">
            <div class="name">{{ item.name }}</div>
            <div class="status">
              <span>发起时间：</span>
              <span>{{ item.originTime }}</span>
            </div>
            <div class="status">
              <span>发起人：</span>
              <span>{{ item.origin }}</span>
            </div>
            <div class="status">
              <span>当前审批环节：</span>
              <span>{{ item.node }}</span>
            </div>
            <div class="status">
              <span>流程状态：</span>
              <span class="status-span" :class="`status-span-${item.status}`">{{ ResultsReviewStatus[item.status] }}</span>
            </div>
          </a>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";
import Bpm from "@/api/bpm";
import Enum from "@/typings/Enum";

@Component({
  name: "BusinessResultsReviewList",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh
  }
})
export default class BusinessResultsReviewList extends Base {
  private listSource: any[] = [];
  private loading = false;
  private finished = false;
  private refreshing = false;
  private ResultsReviewStatus = Enum.ResultsReviewStatus;

  private apiParams = {
    pageNum: 1,
    pageSize: 20,
    sortBy: "originTime", // 排序字段
    order: "desc", // 排序顺序
    businessId: "",
    name: "",
    type: 1,
    status: "",
    originTimeStart: "",
    originTimeEnd: "",
    searchValue: "" // 搜索内容
  };

  private created() {
    this.apiParams.businessId = (this.$route.query.businessId ? this.$route.query.businessId : "") as string;
  }

  /**
   * 跳转业务概况
   */
  private async goto(item: any) {
    if (item && item.instanceH5url) {
      const res: any = await Bpm.GetBpmValidityTime();
      if (res && res.state == 200) {
        const bpmCode = res.data;

        // 跳转bpmurl组装 START
        let url = "";
        if (item.status === "1") {
          url = item.instanceH5url && `${item.instanceH5url}&workState=1`;
        } else {
          if (this.currentUser.jobNo === item.originNo) {
            url = item.h5Url && `${item.h5Url}`;
          } else {
            url = item.instanceH5url && `${item.instanceH5url}&workState=2`;
          }
        }

        if (url) {
          if (!/[&|?]InstanceId=/.test(url)) {
            url += `&InstanceId=${item.instanceId}`;
          }
          url += `&IsMobile=true&date=${bpmCode}&code=${this.currentUser.jobNo}`;
          location.href = url;
        } else {
          this.$dialog({ message: "跳转链接有误，请检查！" });
        }
        // 跳转bpmurl组装 END
      }
    }
  }

  onSearch(event: any) {
    this.apiParams.searchValue = event;
    this.refreshing = true;
    this.onRefresh();
  }

  onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.apiParams.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetBusinessResultsReviewList(this.listParams);
      if (res && res.code === 200) {
        if (this.apiParams.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.apiParams.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  /**
   * 预设变量，可能后期扩展可以用到
   */
  get listParams() {
    const _return = this.apiParams;
    return _return;
  }
}
</script>

<style lang="less" scoped>
.business-list {
  .search-div {
    background-color: white;
    display: flex;
    align-items: center;
    .iconshaixuan {
      padding: 10px 10px 10px 0px;
      font-size: 22px;
      color: #bfbfbf;
    }
    .search-one-div {
      flex: 1;
    }
  }

  .list-item {
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    // height: 125px;
    display: block;
    padding: 15px;
    position: relative;
    &::before {
      top: 18px;
      left: 0px;
      content: "";
      position: absolute;
      width: 3px;
      height: 14px;
      background: linear-gradient(281deg, #5a96fa -338%, #6dc1fd 93%);
    }
    .name {
      font-size: 16px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      word-break: break-all;
    }
    .status {
      margin-top: 10px;
      display: flex;
      align-content: center;
      color: #717787;
    }
    .status-span {
      padding: 0 6px;
      height: 18px;
      line-height: 18px;
      text-align: center;
      font-size: 12px;
      color: white;
      border-radius: 18px;
      background: #36cfc9;
    }
    .status-span-0 {
      background: #36cfc9;
    }
    .status-span-1 {
      background: #1989fa;
    }
    .status-span-2 {
      background: #9ea9bc;
    }
    .date {
      color: #a1a6b3;
      margin-top: 8px;
    }
  }
}
</style>
